package com.ch.movie.adapter

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.ch.movie.R
import com.ch.movie.ui.SampleCoverVideo
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import java.util.*

class NormalViewHolder(private val context: Context, itemView: View) :
    RecyclerView.ViewHolder(itemView) {
    companion object {
        const val TAG = "NormalViewHolder.TAG"
    }

    private val mGSYVideoOptionBuilder: GSYVideoOptionBuilder = GSYVideoOptionBuilder()
    val mNormalGSYVideoPlayer: SampleCoverVideo = itemView.findViewById(R.id.gsy_video_player)

    fun onBind(title: String, url: String, path: String) {
        val header: MutableMap<String, String> = HashMap()
        header["ee"] = "33"
        //防止错位，离开释放
        mNormalGSYVideoPlayer.initUIState();
        mGSYVideoOptionBuilder
            .setIsTouchWiget(false) //.setThumbImageView(imageView)
            .setUrl(url)
            .setVideoTitle(title)
            .setCacheWithPlay(false)
            .setRotateViewAuto(true)
            .setLockLand(true)
            .setPlayTag(TAG)
            .setMapHeadData(header)
            .setShowFullAnimation(true)
            .setNeedLockFull(true)
            .setPlayPosition(position)
            .setVideoAllCallBack(object : GSYSampleCallBack() {
                override fun onPrepared(url: String, vararg objects: Any) {
                    super.onPrepared(url, *objects)
                    if (!mNormalGSYVideoPlayer.isIfCurrentIsFullscreen) {
                        //静音
                        GSYVideoManager.instance().isNeedMute = true
                    }
                }

                override fun onQuitFullscreen(url: String, vararg objects: Any) {
                    super.onQuitFullscreen(url, *objects)
                    //全屏不静音
                    GSYVideoManager.instance().isNeedMute = true
                }

                override fun onEnterFullscreen(url: String, vararg objects: Any) {
                    super.onEnterFullscreen(url, *objects)
                    GSYVideoManager.instance().isNeedMute = false
                    mNormalGSYVideoPlayer.currentPlayer.titleTextView.text = objects[0] as String
                }
            }).build(mNormalGSYVideoPlayer)

        //增加title
        mNormalGSYVideoPlayer.titleTextView.visibility = View.GONE

        //设置返回键
        mNormalGSYVideoPlayer.backButton.visibility = View.GONE

        //设置全屏按键功能
        mNormalGSYVideoPlayer.fullscreenButton
            .setOnClickListener { resolveFullBtn(mNormalGSYVideoPlayer) }
        mNormalGSYVideoPlayer.loadCoverImage(path, R.mipmap.xxx2)
    }

    /**
     * 全屏幕按键处理
     */
    private fun resolveFullBtn(standardGSYVideoPlayer: StandardGSYVideoPlayer) {
        standardGSYVideoPlayer.startWindowFullscreen(context, true, true)
    }

    fun getPlayer(): SampleCoverVideo {
        return mNormalGSYVideoPlayer
    }
}